<?php
class AdImagesController extends AppController {

	public $name    = 'AdImages';
    public $helpers = array('Javascript');
    public $uploadPath;
    public $savePath;
	public $scaffold;

    public function beforeFilter () {
        parent::beforeFilter();
        $this->uploadPath = WWW_ROOT.'img'.DS.'tmp'.DS;
        $this->savePath   = WWW_ROOT.'img'.DS.'images'.DS;
    } // beforeFilter

    public function admin_index () {
        $params = array(
            'recursive' => 0,
            'limit'     => 10
        );
        $this->paginate = $params;
        $this->set('adImages', $this->paginate());
    } // admin_index

    public function axUpload () {
        Configure::write('debug', 0);
        App::import('Vendor', 'Image', array('file'=>'image.class.php'));
        $json = $_POST;
        $name = basename($_FILES['uploadImage']['tmp_name']);

        $img = new Upload($_FILES['uploadImage']);

        $img->file_max_size      = 4096000;
        $img->allowed            = array('image/jpeg', 'image/pjpeg', 'image/gif', 'image/png');
        $img->file_new_name_body = $name;
        $img->image_min_width    = 64;
        $img->image_min_height   = 64;
        $img->image_resize       = true;
        $img->image_x            = 300;
        $img->image_y            = 300;
        $img->image_ratio        = true;
        $img->Process($this->uploadPath);

        if ($img->processed) {
            $json['status'] = 1;
            $filename       = $img->file_dst_name;

            $this->Session->write('Upload.image'.$_POST['imageNo'], array(
                'filename' => $filename,
                'original' => $_FILES['uploadImage']['name'])
            );

            $img->file_new_name_body = 's_'.$name;
            $img->image_resize       = true;
            $img->image_x            = 64;
            $img->image_y            = 64;
            $img->image_ratio        = true;
            //$img->image_ratio_crop   = true;
            $img->Process($this->uploadPath);
            $json['filename'] = ($img->processed) ? $img->file_dst_name : $filename;
            $img->Clean();
        } else {
            $json['status']  = -1;
            $json['message'] = __($img->error, true);
        }

        $this->set('json', $json);
        $this->render(false, 'json');
        //sleep(1);
    } // axUpload

    public function axDelete ($id) {
        Configure::write('debug', 0);
        $json   = array();
        $status = -1;

        if ($this->Session->Check('Upload.image'.$id)) {
            $image    = $this->Session->read('Upload.image'.$id);
            $filename = $image['filename'];
            $json['filename'] = $filename;

            $this->Session->del('Upload.image'.$id);
            @unlink($this->uploadPath.$filename);
            @unlink($this->uploadPath.'s_'.$filename);
            $status = 1;
        }

        $json['status'] = $status;
        header('X-JSON: { status:'.$status.' }');

        $this->set('json', $json);
        $this->render(false, 'json');
        //sleep(1);
    } // axDelete

    public function axDelete2 ($id) {
        Configure::write('debug', 0);
        $json   = array();
        $status = -1;

        if ($id) {
            $image = $this->AdImage->findById($id, array('fields'=>'AdImage.filename, AdPost.id, AdPost.ad_image_id'));

            if ($this->AdImage->del($id)) {
                $status = 1;
                @unlink($this->savePath.$image['AdImage']['filename']);
                @unlink($this->savePath.'s_'.$image['AdImage']['filename']);

                if ($image['AdPost']['ad_image_id'] == $id) { // deleted image was default image in post
                    $img = $this->AdImage->findByAdPostId( // get another image in same post
                        $image['AdPost']['id'],
                        array('recursive'=>0, 'fields'=>'AdImage.id')
                    );
                    $newid = $img['AdImage']['id'];
                    $newid = ($newid) ? $newid : null;

                    App::import('Model', 'AdPost');
                    $AdPost = new AdPost();
                    $AdPost->id = $image['AdPost']['id'];
                    $AdPost->saveField('ad_image_id', $newid);
                }
            }
        }

        $json['status'] = $status;
        header('X-JSON: { status:'.$status.' }');

        $this->set('json', $json);
        $this->render(false, 'json');
    } // axDelete2

} // AdImagesController
?>